//
//  Kratos_MenuTableView.m
//  kk_framework
//
//  Created by 7yao.top on 2018/11/5.
//  Copyright © 2018年 7yao.top. All rights reserved.
//

#import "Kratos_MenuViewController.h"
#import "Kratos_baseCellModel.h"
#import "Kratos_BaseTableViewCell.h"
#import "UIView+kkk_viewExtionsion.h"
#import "Kratos_TableViewControllerConst.h"
#import "Masonry.h"
#import "Kratos_titleCellModel.h"

@interface Kratos_MenuViewController () <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UIView *sectionHeaderView;

@end

@implementation Kratos_MenuViewController
singleton_m(Kratos_MenuViewController)
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    [self.Kra_coverView addSubview:self.Kra_tableView];

    

    [self Kratos_setupTableViewConstraint];
    
    
    Kratos_titleCellModel *titleModel = [[Kratos_titleCellModel alloc] initWithTitle:@"fasd" Kratos_iconName:@"agree" Kratos_actionBlock:^(Kratos_baseCellModel *model) {
        
    }];
    
    Kratos_titleCellModel *titleModel1 = [[Kratos_titleCellModel alloc] initWithTitle:@"fasd" Kratos_iconName:@"agree" Kratos_actionBlock:^(Kratos_baseCellModel *model) {
        
    }];
    
    Kratos_titleCellModel *titleModel2 = [[Kratos_titleCellModel alloc] initWithTitle:@"asdfd" Kratos_iconName:@"agree" Kratos_actionBlock:^(Kratos_baseCellModel *model) {
    
    }];

    NSMutableArray *section =  [NSMutableArray arrayWithObjects:titleModel,titleModel1,titleModel2, nil];
    [self.Kra_dataArray addObject:section];
    [self.Kra_tableView reloadData];
    
}

- (UIView *)Kratos_tableVieHeaderView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 50)];
    
    return  view;
}


 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self Kratos_hideMenuView];

}
- (void)Kratos_hideMenuView
{
    [self dismissViewControllerAnimated:false completion:nil];
}

- (UITableView *)Kra_tableView
{
    if (_Kra_tableView == nil) {
        _Kra_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _Kra_tableView.translatesAutoresizingMaskIntoConstraints = NO;
        _Kra_tableView.backgroundColor = [UIColor whiteColor];
        _Kra_tableView.delegate = self;
        _Kra_tableView.dataSource = self;
        _Kra_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _Kra_tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        _Kra_tableView.showsVerticalScrollIndicator = NO;
        
        if(floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_8_x_Max){
            _Kra_tableView.cellLayoutMarginsFollowReadableWidth = NO;
        }
    }
    return _Kra_tableView;
}

- (void)Kratos_setupTableViewConstraint
{
    [self.Kra_tableView  mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.view.mas_top);
        make.left.mas_equalTo(self.view.mas_left);
        make.bottom.mas_equalTo(self.view.mas_bottom);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.5);
        
    }];
}

- (NSMutableArray *)Kra_dataArray
{
    if(_Kra_dataArray == nil){
        _Kra_dataArray = [NSMutableArray array];
    }
    return _Kra_dataArray;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.Kra_dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSMutableArray *rows = [self.Kra_dataArray objectAtIndex:section];
    NSAssert([rows isKindOfClass:[NSMutableArray class]], @"此对象必须为一个可变数组,请检查数据源组装方式是否正确!");
    return rows.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *sections = [self.Kra_dataArray objectAtIndex:indexPath.section];
    NSAssert([sections isKindOfClass:[NSMutableArray class]], @"此对象必须为一个可变数组,请检查数据源组装方式是否正确!");
    Kratos_baseCellModel *cellModel = (Kratos_baseCellModel *)[sections objectAtIndex:indexPath.row];
    
    Class class = NSClassFromString(cellModel.Kra_cellClass);
    NSAssert([class isSubclassOfClass:[Kratos_BaseTableViewCell class]], @"此cellclass类别必须存在,并且继承Kratos_BaseTableViewCell");
    Kratos_BaseTableViewCell *cell = [class Kratos_cellWithIdentifier:cellModel.Kra_cellClass Kratos_tableView:tableView];
    [cell Kratos_setupDataModel:cellModel];
    cell.topLine.hidden = indexPath.row != 0;
    [cell.bottomLine setKra_x:(indexPath.row == sections.count - 1 ? 0:cellModel.Kra_separateOffset)];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *sections = [self.Kra_dataArray objectAtIndex:indexPath.section];
    NSAssert([sections isKindOfClass:[NSMutableArray class]], @"此对象必须为一个可变数组,请检查数据源组装方式是否正确!");
    Kratos_baseCellModel *cellModel = (Kratos_baseCellModel *)[sections objectAtIndex:indexPath.row];
    Class class =  NSClassFromString(cellModel.Kra_cellClass);
    return [class Kratos_getCellHeight:cellModel];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *sections = [self.Kra_dataArray objectAtIndex:indexPath.section];
    NSAssert([sections isKindOfClass:[NSMutableArray class]], @"此对象必须为一个可变数组,请检查数据源组装方式是否正确!");
    Kratos_baseCellModel *cellModel = (Kratos_baseCellModel *)[sections objectAtIndex:indexPath.row];
    [tableView deselectRowAtIndexPath:indexPath animated:cellModel.Kra_actionBlock == nil];
    if(cellModel.Kra_actionBlock){
        cellModel.Kra_actionBlock(cellModel);
    }
}

#pragma mark tableView代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return  0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //如果是第一个section
    if (section == 0) return nil;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, Kratos_SectionHeight)];
    [view setBackgroundColor:[UIColor clearColor]];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    //默认
    if(section == 0) return 0.01;
    return Kratos_SectionHeight;
}


- (void)Kratos_updateCellModel:(Kratos_baseCellModel *)cellModel
{
    [self updateCellModel:cellModel animation:UITableViewRowAnimationFade];
}
- (void)updateCellModel:(Kratos_baseCellModel *)cellModel animation:(UITableViewRowAnimation)animation
{
    //这里根据模型标题是否一样，是更新的哪个model
    NSMutableArray *tempData = [NSMutableArray arrayWithArray:self.Kra_dataArray];
    [tempData enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx1, BOOL * _Nonnull stop) {
        NSMutableArray *sections = (NSMutableArray *)obj;
        NSAssert([sections isKindOfClass:[NSMutableArray class]], @"此对象必须为一个可变数组,请检查数据源组装方式是否正确!");
        [sections enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx2, BOOL * _Nonnull stop) {
            Kratos_baseCellModel *model  = (Kratos_baseCellModel *)obj;
            if([model.Kra_identifier isEqualToString:cellModel.Kra_identifier]){
                //找到section中的数组
                NSMutableArray *rows = [self.Kra_dataArray objectAtIndex:idx1];
                //找到某个具体哪一行，进行数据替换
                [rows replaceObjectAtIndex:idx2 withObject:cellModel];
                //更新cell
                NSIndexPath *path = [NSIndexPath indexPathForRow:idx2 inSection:idx1];
                [self.Kra_tableView beginUpdates];
                [self.Kra_tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:animation];
                [self.Kra_tableView endUpdates];
                *stop = YES;
                return;
            }
        }];
    }];
}

//- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
//{
//    NSLog(@"控制器方法");
//    [self.Kra_dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        NSMutableArray *sections = (NSMutableArray *)obj;
//        NSAssert([sections isKindOfClass:[NSMutableArray class]], @"此对象必须为一个可变数组,请检查数据源组装方式是否正确!");
//
//        
//    }];
//}

@end







